use std::sync::OnceLock;
use sqlx::mysql::MySqlPool;

use crate::config::DbConfig;

pub static SQLX_POOL: OnceLock<MySqlPool> = OnceLock::new();

pub async fn init(config: &DbConfig) {
    println!("^^^^ connect to mysql with url: {}", config.url);
    let sqlx_pool = MySqlPool::connect(&config.url).await
        .expect("Database connection failed.");
    crate::db::SQLX_POOL
        .set(sqlx_pool)
        .expect("sqlx pool should be set")
    }

pub fn pool() -> &'static MySqlPool {
    SQLX_POOL.get().expect("sqlx pool should be set")
}
